import 'package:flutter/material.dart';
import 'package:jdapp/widget/jdButton.dart';
import 'package:provider/provider.dart';
import 'package:jdapp/provider/Counter.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jdapp/services/UserServices.dart';
import '../../services/EventBus.dart';

class User extends StatefulWidget {
  const User({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<User> {
  @override
  bool isLogin = false;
  List userInfo = [];
  void initState() {
    super.initState();
    //_getUserinfo();
    eventBus.on<UserEvent>().listen((event) {
      print(event.str);
      this._getUserinfo();
    });
  }

  _getUserinfo() async {
    var isLogin = await UserServices.getUserLoginState();
    var userInfo = await UserServices.getUserInfo();

    setState(() {
      this.userInfo = userInfo;
      this.isLogin = isLogin;
    });
  }

  Widget build(BuildContext context) {
    var counterProvider = Provider.of<Counter>(context);
    return Scaffold(
      body: ListView(
        children: [
          Container(
            height: 220.h,
            width: double.infinity,
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage("images/user_bg.jpg"),
                    fit: BoxFit.cover)),
            child: Row(
              children: [
                ClipOval(
                  child: Image.asset(
                    'images/user.png',
                    fit: BoxFit.cover,
                    width: 100.w,
                    height: 100.h,
                  ),
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text("用户名：12334",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 32.sp)),
                    Text("普通会员",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 24.sp))
                  ],
                ),
              ],
            ),
          ),
          ListTile(
            leading: Icon(
              Icons.assignment,
              color: Colors.red,
            ),
            title: Text("全部订单"),
          ),
          Divider(),
          ListTile(
            leading: Icon(Icons.payment, color: Colors.green),
            title: Text("待付款"),
          ),
          Divider(),
          ListTile(
            leading: Icon(Icons.local_car_wash, color: Colors.orange),
            title: Text("待收货"),
          ),
          Container(
              width: double.infinity,
              height: 10,
              color: Color.fromRGBO(242, 242, 242, 0.9)),
          ListTile(
            leading: Icon(Icons.favorite, color: Colors.lightGreen),
            title: Text("我的收藏"),
          ),
          Divider(),
          ListTile(
            leading: Icon(Icons.people, color: Colors.black54),
            title: Text("在线客服"),
          ),
          Divider(),
          JdButton(
            text: "退出登录",
            // cb: () {
            //   UserServices.loginOut();
            //   _getUserinfo();
            // }
          )
        ],
      ),
    );
  }
}
